<?php
require_once("../DAO/UserDAO.php");
include_once("../DAO/CategoryDAO.php");
include_once("../DAO/QuestionDAO.php");
include_once("../Entities/Question.php");
include_once("../Utility/WebUtilities.php");
include_once("../Utility/WebSession.php");

if(WebSession::CurrentUserNoReturn() == null || WebSession::CurrentUserNoReturn()->Role != WebUtilities::GetAdminRoleId())
{
	WebUtilities::Redirect("index.php");
}

$categoryDao = new CategoryDAO();
$questionDao = new QuestionDAO();

$paragraphId = WebUtilities::GetQueryString("paragraphId", 0);
if($paragraphId == 0)
{
	$categoryId = WebUtilities::GetQueryString("category", 0);
	$pageSize = WebUtilities::GetPageSize();
	$page = WebUtilities::GetQueryString("page", 1);
	$totalPage = 0;
	$questionList = $questionDao->GetQuestions($page, $pageSize, $totalPage, $categoryId);
}
else
{
	$categoryId = 0;
	$pageSize = WebUtilities::GetPageSize();
	$page = 1;
	$totalPage = 1;
	$questionList = $questionDao->GetQuestionsByParagraphId($paragraphId);
}
$categoryList = $categoryDao->GetAll();
$optionStr = "<option value='0'>All</option>";
for($i = 0; $i < count($categoryList); $i++)
{
	if($categoryId == $categoryList[$i]->Id)
		$selectedIndex = "selected='true'";
	else
		$selectedIndex = "";
	$optionStr = $optionStr . "<option value='" . $categoryList[$i]->Id . "' " . $selectedIndex . ">" . $categoryList[$i]->Name . "</option>";
} 

$questionStr = "";
if(count($questionList) == 0)
{
	$questionStr = "<tr><td colspan='6'>No items</td></tr>";
}
else
{
	for($i = 0; $i < count($questionList); $i++)
	{
		$item = $questionList[$i];
		$category = $categoryDao->GetById($item->Category);
		if($item->Sound != "")
		{
			$soundStr = "<td>Yes</td>";
		}
		else
		{
			$soundStr = "<td>No</td>";
		}
		$questionStr = $questionStr . "<tr><td class='action'><input type='checkbox' onclick='checkProcess.checkItem(this.checked)' class='checkItems' name='checkItems" . $i . "' value='" . $item->Id . "'/></td>"
									. "<td>" . $item->Id . "</td>"
									. "<td><a href='#' onclick=\"viewQuestionContent('" . $item->Id . "')\">View Detail</a></td>"
									. "<td>" . $category->Name . "</td>"
									. $soundStr
									. "<td><a href='EditQuestion.php?paragraphId=" . $paragraphId . "&id=" . $item->Id . "'>Edit</a></td></tr>"; 
	}
}

$submitUrl = "QuestionManagement.php?page=" . $page . "&category=" . $categoryId;
$pageUrlFirst = "<a href='QuestionManagement.php?category=" . $categoryId . "&page=1'>First</a>";
$pageUrlLast = "<a href='QuestionManagement.php?category=" . $categoryId . "&page=" . $totalPage ."'>Last</a>";
$pageUrlNext = "<a href='QuestionManagement.php?category=" . $categoryId . "&page=" . ($page + 1) ."'>Next ></a>";
$pageUrlPrev = "<a href='QuestionManagement.php?category=" . $categoryId . "&page=" . ($page - 1) ."'>< Prev</a>";
if($page <= 1)
{
	$pageUrlFirst = "First";
	$pageUrlPrev = "< Prev";
}
if($page >= $totalPage)
{
	$pageUrlLast = "Last";
	$pageUrlNext = "Next >";
}

$mainContent = "<form enctype='multipart/form-data' action='" . $submitUrl ."' method='POST'>
					<div class='oneLine'>
						<div class='left'>Categories</div>
						<select class='right' id='cbCategories' " . ($paragraphId != 0 ? "disabled='true'" : "") . ">"
							. $optionStr . 	
						"</select>
						<input value='Search' type='button' " . ($paragraphId != 0 ? "disabled='true'" : "") ." onclick='SubmitSearchQuestion()'/>
					</div>
					<br/>
					<table class='manager' id='tableAnswers'>
						<thead>
							<tr>
								<th><input type='checkbox' id='checkAllItem' name='checkAllItem' onclick='checkProcess.checkAll(this.checked)' /></th>
								<th>Question Id</th>
								<th>Content</th>
								<th>Category</th>
								<th>Sound</th>
								<th>Action</th>
							</tr>
						</thead>
						<tbody class='tableBody'>"
							. $questionStr . 
						"</tbody>
					</table>" .
				"<div class='paging'>
					Page " . ($page >= $totalPage ? $totalPage : $page) . " of " . $totalPage . " " . 
					$pageUrlFirst . " | " . $pageUrlPrev . " | " . $pageUrlNext . " | " . $pageUrlLast . 
				"</div><div class='paging'>
					<div>
						<input type='submit' value='Delete' name='btDelete' />
						<input type='button' value='Add' onclick=\"window.location='EditQuestion.php" . ($paragraphId != 0 ? "?paragraphId=" . $paragraphId  : "") . "';\" />
					</div>
				</div></form>
				<div id='popupPanel' class='popupPanel'>
					<a href='#' onclick='ClosePopupPanel(); return false;'>Close</a>
					<div id='panelContent' style='padding:10px 10px 10px 10px;overflow:auto;'></div>
				</div>";
$scriptContent = "checkProcess.totalItems = " . count($questionList) . ";" . 
	"checkProcess.checkAllItems = document.getElementById('checkAllItem');";
$pageIndex = 5;
include("MasterPage.php");
?>